
import os
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
import facebook
import logging
import secret

api_key = "0e3a4168c5e69943f8a4b9fdf626d66f"
secret_key = secret.secret_key
app_url = "http://apps.facebook.com/wedding-tree/"

class Wedding(db.Model):
    participants = db.ListProperty(str)

class FacebookCallback(webapp.RequestHandler):
    def post(self,*args):
        self.callback_template_data = {}
        self.callback_template_path = self.__class__.__name__ + ".html" 

        self.facebookapi = facebook.Facebook(api_key, secret_key)

        if not self.facebookapi.check_session(self.request):
            url = app_url
            self.response.out.write('<fb:redirect url="' + url + '" />')
            return
        if not self.facebookapi.added:
            url = self.facebookapi.get_add_url()
            self.response.out.write('<fb:redirect url="' + url + '" />')
            return
        self.callback(*args)
        self.render()
        
    def render(self):
        path = os.path.join(os.path.dirname(__file__), 'templates', 'callback', self.get_callback_template_path())
        self.response.out.write(template.render(path, self.get_callback_template_data()))
    def callback(self): pass
    def get_callback_template_path(self): return self.callback_template_path
    def get_callback_template_data(self): return self.callback_template_data

class home(FacebookCallback):
    def callback(self):
        user = self.facebookapi.users.getInfo( [self.facebookapi.uid], ['uid', 'name'])[0]
        self.callback_template_data.update({"title":"Wedding Tree - Home","user":user})

class find_wedding(FacebookCallback):
    def callback(self):
        fuid = self.request.get('fuid')
        #fuid_selector = self.request.get('fuid-selector')
        #logging.info('fuid: %s' % fuid)
        #logging.info('fuid-selector: %s' % fuid_selector)
        query = Wedding.all()
        query.filter('participants =', fuid)
        weddings = query.fetch(1000)
        self.callback_template_data.update({"title":"Wedding Tree - Find Wedding",
                                            "weddings":weddings,
                                            "fuid":fuid})

class new_wedding(FacebookCallback):
    def callback(self):
        fuid = self.request.get('fuid')
        #logging.info('fuid: %s' % fuid)
        self.callback_template_data.update({"title":"Wedding Tree - Create Wedding",
                                            "fuid":fuid})

class add_wedding(FacebookCallback):
    def callback(self):
        #TODO: ensure this set of participants does not have a wedding
        #TODO: figure out how to map the same people getting married twice...
        fuid = self.request.get('fuid')
        puid = self.request.get('puid')
        self.wedding = Wedding(participants=[fuid,puid])
        self.wedding.put()
    def render(self):
        url = app_url + 'show_wedding/' + str(self.wedding.key().id())
        self.response.out.write('<fb:redirect url="' + url + '" />')

class show_wedding(FacebookCallback):
    def callback(self,wid):
        wedding = Wedding.get_by_id(long(wid))
        self.callback_template_data.update({"title":"Wedding Tree - Show Wedding",
                                            "wedding":wedding})

application = webapp.WSGIApplication(
                                     [('/callback/', home),
                                      ('/callback/find_wedding', find_wedding),
                                      ('/callback/new_wedding', new_wedding),
                                      ('/callback/add_wedding', add_wedding),
                                      ('/callback/show_wedding/([-\w]+)', show_wedding)],
                                     #[('/callback/add-wedding', AddWeddingCallback)],
                                     #[('/callback/show_wedding', ShowWeddingCallback)],
                                     #[('/callback/tag-participant', TagParticipantCallback)],
                                     #[('/callback/remove-participant', RemoveParticipantCallback)],
                                     #[('/callback/remove-wedding', RemoveWeddingCallback)],
                                     debug=True)

def main():
    logging.getLogger().setLevel(logging.DEBUG)
    run_wsgi_app(application)

if __name__ == "__main__": 
    main()
